package com.ymatou.iapi.sellercs.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellercs.parameter.SyncOnlineSellerIdBean;
import com.ymatou.iapi.sellercs.service.SyncOnlineSellerIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;

/**
*同步在线买手
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SyncOnlineSellerId{
	private static SyncOnlineSellerIdBean synconlineselleridBean;
	private static SyncOnlineSellerIdCall synconlineselleridCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("同步在线买手");
	}

	@Before
	public void caseUp() {
		synconlineselleridBean=new SyncOnlineSellerIdBean();
		synconlineselleridCall=new SyncOnlineSellerIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(synconlineselleridCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SyncOnlineSellerId_001() {
		Logger.start(true,"同步在线买手,成功");
		try {
			String token=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(sellerId, sellerId, token);
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			getLog.startListenVm("default.log",".");
			synconlineselleridBean.setAppId("auto");
			synconlineselleridCall.setData(synconlineselleridBean);
			synconlineselleridCall.callService();			
			Logger.verifyEquals("ok", synconlineselleridCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
//			loglist.forEach(log->{Logger.debug(log);});
			
			Logger.comment("验证日志包含发送指令");
			List<String> filter2 = loglist.stream().filter(s->s.contains("PC在线买手数：")).collect(Collectors.toList());
			Logger.verifyEquals(1, filter2.size(), "验证日志数量");
			String [] msg=filter2.get(0).split("：");
			Integer count=Integer.valueOf(msg[msg.length-1]);
			Logger.verifyEquals(true, count>=1, "验证同步");
			//Thread:main 2018-10-19 14:18:59.013 2018-10-19 14:18:58,112 [taskExecutor-2] INFO  c.y.sellercs.domain.service.JobService KEY[SyncLoginSellerIdReq|97f157abe1a04c5ebd46fa7d018d9eb7] [21deca0556f98af0] - PC登录买手数：1
			SellerCSIMCallService.OffLine(sellerId, token);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	//这个case建议手动执行，不能保证整个环境只有一个买手在线
	public void Tc_SyncOnlineSellerId_002() {
		Logger.start(true,"同步在线买手,买手暂时离开,不同步");
		try {
			String token=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(sellerId, sellerId, token);
			SellerCSIMCallService.LeaveLine(sellerId);
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			getLog.startListenVm("default.log",".");
			synconlineselleridBean.setAppId("auto");
			synconlineselleridCall.setData(synconlineselleridBean);
			synconlineselleridCall.callService();			
			Logger.verifyEquals("ok", synconlineselleridCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
//			loglist.forEach(log->{Logger.debug(log);});
			
			Logger.comment("验证日志包含发送指令");
			List<String> filter2 = loglist.stream().filter(s->s.contains("PC在线买手数：")).collect(Collectors.toList());
			Logger.verifyEquals(1, filter2.size(), "验证日志数量");
			String [] msg=filter2.get(0).split("：");
			Integer count=Integer.valueOf(msg[msg.length-1]);
			Logger.verifyEquals(true, count==0, "验证同步");
			//Thread:main 2018-10-19 14:18:59.013 2018-10-19 14:18:58,112 [taskExecutor-2] INFO  c.y.sellercs.domain.service.JobService KEY[SyncLoginSellerIdReq|97f157abe1a04c5ebd46fa7d018d9eb7] [21deca0556f98af0] - PC登录买手数：1
			SellerCSIMCallService.OffLine(sellerId, token);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}